﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QLHPHP
{
    public partial class DangKyMonHocControl : UserControl
    {
        public DangKyMonHocControl()
        {
            InitializeComponent();
        }

        private AppCommands _Commands;
        
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public AppCommands Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    AppCommands oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(AppCommands oldValue, AppCommands newValue)
        {
            if (newValue != null)
            {
                buttonDong.Command = newValue.DangKyMonHocCommands.Cancel;
            }
            else
            {
                buttonDong.Command = null;
            }
        }

        private void btnChiTietDKMonHoc_Click(object sender, EventArgs e)
        {
            btnChiTietDKMonHoc.Command = _Commands.ChiTietDangKyCommands.New;
            
        }

        
        private void DangKyMonHocControl_Load(object sender, EventArgs e)
        {
            txtspht.DataBindings.Add("text", MonHoc.LaySPMax(), "LonNhat");
            LoadDSKhoaInCbb();
            LoadDSNganhInCbb();
        }

        //Load list khoa vao cbb
        private void LoadDSKhoaInCbb()
        {
            try
            {

                cbbKhoa.DataSource = Khoa.LayDsKhoa();
                cbbKhoa.ValueMember = "MaKhoa";
                cbbKhoa.DisplayMember = "TenKhoa";
            }
            catch
            {
                ToastNotification.Show(this, "Không thể load danh sách tỉnh");
            }
        }

        //Load danh sach nganh len cbb
        private void LoadDSNganhInCbb()
        {
            try
            {
                string MK = cbbKhoa.SelectedValue.ToString();
                cbbNganh.DataSource = Nganh.LayNganhTheoKhoa(MK);
                cbbNganh.ValueMember = "MaNganh";
                cbbNganh.DisplayMember = "TenNganh";
            }
            catch
            {
                string mk = cbbKhoa.SelectedValue.ToString();
                ToastNotification.Show(this, "Lỗi" + mk + "");
            }
        }

        private void CapNhatDSMonHoc()
        {
            try
            {
                string manganh = cbbNganh.SelectedValue.ToString();   
                dgvDSMHDK.DataSource = MonHoc.LayDSMonHocTheoNganh(manganh);
            }
            catch
            {
                
            }
        }

        public void btnDangKyMonHoc_Click(object sender, EventArgs e)
        {
            if(txtMSSV.Text=="")
            {
                ToastNotification.Show(this, "Chưa nhập mã sinh viên");
            }
            else
            {
                int MaSV = int.Parse(txtMSSV.Text);
               // int SoPhieu = int.Parse(txtSoPhieuDK.Text);
                int SoPhieu = int.Parse(txtspht.Text) + 1;
                int day = DateTime.Now.Day;
                int month = DateTime.Now.Month;
                int year = DateTime.Now.Year;
                string ngaydk = day + "/" + month + "/" + year;
                DateTime ngdk = DateTime.Parse(ngaydk);

                try
                {
                    MonHoc.ThemPhieuDK(SoPhieu, MaSV, ngdk);
                    ToastNotification.Show(this, "Đã đăng ký thành công!");

                    for (int i = 0; i < dgvDSMHDK.Rows.Count - 1; i++)
                    {
                        if (Convert.ToBoolean(dgvDSMHDK.Rows[i].Cells["ChonCot"].Value) == true)
                        {
                            string MaMH = Convert.ToString(dgvDSMHDK.Rows[i].Cells[1].Value);
                            MonHoc.ThemPhieuDKChiTiet(SoPhieu, MaMH);
                        }
                        
                    }
                }
                catch
                {
                    ToastNotification.Show(this, "Lỗi!");
                }
            }
        }

        private void cbbKhoa_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadDSNganhInCbb();
        }

        private void cbbNganh_SelectedValueChanged(object sender, EventArgs e)
        {
            CapNhatDSMonHoc();
        }

       
      
             
    }
}
